package com.game.doudizhu.controller;

import com.game.doudizhu.common.constants.GameConstants;
import com.game.doudizhu.model.*;
import com.game.doudizhu.service.GameService;
import com.game.doudizhu.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class WebSocketController {

    @Autowired
    private GameService gameService;

    @MessageMapping("/room/{roomId}/join")
    @SendTo("/topic/room/{roomId}")
    public GameRoom handleJoin(@DestinationVariable Long roomId, Player player) {
        return gameService.handlePlayerJoin(roomId, player);
    }

    @MessageMapping("/room/{roomId}/ready")
    @SendTo("/topic/room/{roomId}")
    public GameRoom handleReady(@DestinationVariable Long roomId, Player player) {
        return gameService.handlePlayerReady(roomId, player);
    }

    @MessageMapping("/game/{roomId}/bid")
//    @SendTo("/topic/game/{roomId}")
    public GameState handleBid(@DestinationVariable Long roomId, BidMessage bidMessage) {
        return gameService.handleBid(roomId, bidMessage);
    }

    @MessageMapping("/game/{roomId}/play")
//    @SendTo("/topic/game/{roomId}")
    public GameState handlePlay(@DestinationVariable Long roomId, PlayCardMessage playCardMessage) {
        return gameService.handlePlayCards(roomId, playCardMessage);
    }

    @MessageMapping("/game/{roomId}/pass")
//    @SendTo("/topic/game/{roomId}")
    public GameState handlePass(@DestinationVariable Long roomId, Player player) {
        return gameService.handlePlayerPass(roomId, player.getId());
    }

    @MessageMapping("/room/{roomId}/leave")
    @SendTo("/topic/room/{roomId}")
    public GameRoom handleLeave(@DestinationVariable Long roomId, Player player) {
        // 主动离开房间时，直接调用handlePlayerLeave
        return gameService.handlePlayerLeave(roomId, player.getId(), false);
    }
} 